package im.yisme.view.activities;

import java.util.ArrayList;

import im.yisme.model.Lyrics;
import im.yisme.model.service.GameSystemService;
import im.yisme.model.service.ModelServiceProvider;
import im.yisme.view.service.ViewService;
import im.yisme.view.service.ViewServiceProvider;
import im.yisme.view.tasks.EvaluateTask;
import im.yisme.view.ui.GameResultView;
import im.yisme.view.ui.component.OnClickListener;
import im.yisme.view.ui.component.YButton;
import im.yisme.view.ui.util.ButtonEventListener;
import im.yisme.vo.LyricsVO;
import im.yisme.vo.MusicVO;
import android.app.Activity;
import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.MotionEvent;
public class PlayOverActivity extends Activity{
	
	private ViewService viewCtrl;
	private GameSystemService gameModel;
	
	private GameResultView gameResultView;
	private ButtonEventListener eventListener;
	private YButton againButton, backButton;
	private YButton[] starButtons;
	
	private LyricsVO playedLyrics;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		viewCtrl=ViewServiceProvider.getViewService();
		gameModel = ModelServiceProvider.getGameSystemService();
		playedLyrics = gameModel.getPlayingLyrics();
		
		init();
		setContentView(gameResultView);
	}

	@Override
	protected void onResume(){
		super.onResume();
		viewCtrl.updateTopActivity(this);
	}
	
	protected void onDestroy(){
		super.onDestroy();
		gameResultView.stop();
	} 
	
	protected void onPause(){
		super.onPause();
		gameResultView.pause();
	}
	
	protected void onRestart(){
		super.onRestart();
		gameResultView.resume();
	}
	
	protected void onStop(){
		super.onStop();
		if(playedLyrics.getSource() != Lyrics.ORIGINAL && !playedLyrics.isEvaluated()){
			evaluate();
		}		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return eventListener.onButtonEvent(event);
	}
	
	private void init(){
		gameResultView = new GameResultView(this);
		initButtons();
		initButtonListeners();
		registerButtons();
		
		Intent intent = getIntent();
		
		gameResultView.setData(intent.getIntExtra("score", 0), intent.getFloatExtra("accuracy", 0), intent.getIntExtra("maxCombo", 0), intent.getStringExtra("level"));
	}
	
	private void registerButtons(){
		eventListener = new ButtonEventListener();
		eventListener.registerButton(againButton);
		eventListener.registerButton(backButton);
		
		if(playedLyrics.getSource() != Lyrics.ORIGINAL && !playedLyrics.isEvaluated())
		for (int i=0; i<5; i++){
			eventListener.registerButton(starButtons[i]);
		}
	}
	
	private void initButtons(){
		ArrayList<PointF> againButtonPoints = new ArrayList<PointF>();
		againButtonPoints.add(new PointF(500, 425));
		againButtonPoints.add(new PointF(500, 470));
		againButtonPoints.add(new PointF(630, 470));
		againButtonPoints.add(new PointF(630, 425));
		againButton = new YButton();
		againButton.setPoints(againButtonPoints);
		
		ArrayList<PointF> backButtonPoints = new ArrayList<PointF>();
		backButtonPoints.add(new PointF(750, 430));
		backButtonPoints.add(new PointF(750, 480));
		backButtonPoints.add(new PointF(800, 480));
		backButtonPoints.add(new PointF(800, 430));
		backButton = new YButton();
		backButton.setPoints(backButtonPoints);
		
		
		if(playedLyrics.getSource() != Lyrics.ORIGINAL && !playedLyrics.isEvaluated()){
			starButtons = new YButton[5];
			for (int i=0; i<5; i++){
				ArrayList<PointF> points = new ArrayList<PointF>();
				points.add(new PointF(35 + 75 * i, 120 - 25 * i));
				points.add(new PointF(35 + 75 * i, 180 - 25 * i));
				points.add(new PointF(95 + 75 * i, 180 - 25 * i));
				points.add(new PointF(95 + 75 * i, 120 - 25 * i));
				starButtons[i] = new YButton();
				starButtons[i].setPoints(points);
			}
			gameResultView.setStarVisible(true);
		}
		
	}
	
	private void evaluate(){
		EvaluateTask task = new EvaluateTask();
		
		LyricsVO lyricsVO = gameModel.getPlayingLyrics();
		task.execute(lyricsVO.getId(),gameResultView.getEvaluateScore());
	}
	
	private void initButtonListeners(){
		againButton.setOnClickListener(new OnClickListener() {
			
			public void OnClick() {
				// TODO Auto-generated method stub
				Intent intent = new Intent(PlayOverActivity.this, GameActivity.class);
				
				Intent intent1 = getIntent();		
				LyricsVO lyricsVO = (LyricsVO) intent1.getSerializableExtra("lyricsVO");
				MusicVO musicVO = (MusicVO) intent1.getSerializableExtra("musicVO");
				
				intent.putExtra("lyricsVO", lyricsVO);
				intent.putExtra("musicVO", musicVO);
				
				viewCtrl.jumpTo(intent);
			}
		});
		
		backButton.setOnClickListener(new OnClickListener() {
			
			public void OnClick() {
				// TODO Auto-generated method stub
				Intent intent = new Intent(PlayOverActivity.this, PlayMusicListActivity.class);
				viewCtrl.jumpTo(intent);
			}
		});
		
		if(playedLyrics.getSource() != Lyrics.ORIGINAL && !playedLyrics.isEvaluated()){
			starButtons[0].setOnClickListener(new OnClickListener() {
				
				public void OnClick() {
					// TODO Auto-generated method stub
					gameResultView.setScore(1);
					
				}
			});
			
			starButtons[1].setOnClickListener(new OnClickListener() {
				
				public void OnClick() {
					// TODO Auto-generated method stub
					gameResultView.setScore(2);
					
				}
			});

			starButtons[2].setOnClickListener(new OnClickListener() {
				
				public void OnClick() {
					// TODO Auto-generated method stub
					gameResultView.setScore(3);
					
				}
			});

			starButtons[3].setOnClickListener(new OnClickListener() {
				
				public void OnClick() {
					// TODO Auto-generated method stub
					gameResultView.setScore(4);
					
				}
			});

			starButtons[4].setOnClickListener(new OnClickListener() {
				
				public void OnClick() {
					// TODO Auto-generated method stub
					gameResultView.setScore(5);
					
				}
			});
		}
			
		
	}
}
